/**
 *
 */
package org.hackystat.ui.projectviewer.gwt.client.widgets;

import java.util.List;

import org.hackystat.ui.projectviewer.gwt.client.CSSStyles;
import org.hackystat.ui.projectviewer.gwt.client.controller.ProjectViewerController;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.ClickListenerCollection;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.SourcesClickEvents;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Pavel Senin
 *
 */
public class InvitationListWidget extends Composite implements ClickListener, SourcesClickEvents {

  /** {@literal} CAL_WAIT_IMAGE icon. */
  public static final String MEMBER_IMAGE = "images/member.gif";

  final VerticalPanel outer = new VerticalPanel();
  final HorizontalPanel titlePane = new HorizontalPanel();
  final HorizontalPanel invitationsColumns = new HorizontalPanel();
  final VerticalPanel invitationsRows = new VerticalPanel();
  final Image invitationsPaneImage = new Image(MEMBER_IMAGE);
  final Label invitationsTitleLabel = new Label("Invitations:");
  final Label invitationsTitleDesc = new Label("Invitations to join projects");
  final ListBox invitationsListBox = new ListBox();
  final ClickListenerCollection clickListeners = new ClickListenerCollection();
  final InvitationListWidget instance = this;
  private String selection;

  // Project-concious smart buttons panel
  final InvitationActionsWidget invitationActionsWidget = new InvitationActionsWidget();
  private String action = InvitationActionsWidget.ACTION_VIEW;

  /**
   * Constructor.
   */
  public InvitationListWidget() {
    // register with action listener
    addClickListener(ProjectViewerController.getInstance());
    // back to business
    outer.setStyleName(CSSStyles.PROJECT_LIST_WIDGET_STYLE);
    titlePane.setStyleName(CSSStyles.INVITATIONS_PANE_TITLE_STYLE);
    titlePane.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
    invitationsPaneImage.setStyleName(CSSStyles.CP_IMAGE);
    invitationsColumns.add(invitationsPaneImage);
    invitationsColumns.add(invitationsRows);
    invitationsRows.add(invitationsTitleLabel);
    invitationsTitleLabel.setStyleName(CSSStyles.SETTINGS_TITLE);
    invitationsRows.add(invitationsTitleDesc);
    invitationsTitleDesc.setStyleName(CSSStyles.HOST_EXAMPLE);
    titlePane.add(invitationsColumns);
    outer.add(titlePane);
    invitationsListBox.setVisibleItemCount(4);
    invitationsListBox.setStyleName(CSSStyles.PROJECT_LIST_STYLE);
    outer.add(invitationsListBox);
    invitationsListBox.addClickListener(new ClickListener() {
      public void onClick(Widget sender) {
        setSelection(invitationsListBox.getValue(invitationsListBox.getSelectedIndex()));
        action = ProjectActionsWidget.ACTION_VIEW;
        GWT.log("Firing action " + getAction() + " on project " + getSelection(), null);
        clickListeners.fireClick(instance);
      }
    });

    outer.add(invitationActionsWidget);
    invitationActionsWidget.addClickListener(this);
    initWidget(outer);
  }

  /**
   * Sets the selection into the short-term memory.
   *
   * @param value The selection value.
   */
  private void setSelection(String value) {
    this.selection = value;
  }

  /**
   * Sets the project list.
   *
   * @param index The project list.
   */
  public void setInvitationsIndex(List<String> index) {
    invitationsListBox.setEnabled(false);
    invitationsListBox.clear();
    for (String str : index) {
      invitationsListBox.addItem(str, str);
    }
    invitationsListBox.setEnabled(true);
  }

  /**
   * Reports selection from the short-term memory.
   *
   * @return The selection.
   */
  public String getSelection() {
    return this.selection;
  }

  /**
   * Sets list enabled/disabled.
   *
   * @param b The value to set.
   */
  public void setEnabled(boolean b) {
    invitationsListBox.setEnabled(b);
  }

  /**
   * {@inheritDoc}
   */
  public void onClick(Widget sender) {
    if (sender == invitationActionsWidget) {
      this.action = invitationActionsWidget.getAction();
      GWT.log("Firing action " + getAction() + " on project " + getSelection(), null);
      clickListeners.fireClick(instance);
    }
  }

  /**
   * Reports the action happened.
   *
   * @return The action happened.
   */
  public String getAction() {
    return this.action;
  }

  /**
   * {@inheritDoc}
   */
  public void addClickListener(ClickListener listener) {
    clickListeners.add(listener);
  }

  /**
   * {@inheritDoc}
   */
  public void removeClickListener(ClickListener listener) {
    clickListeners.remove(listener);
  }

  /**
   * Cleanups the invitation list.
   */
  public void clean() {
    invitationsListBox.clear();
  }

  /**
   * Pops all pushed buttons.
   */
  public void resetButtons() {
    invitationActionsWidget.refreshButtons();
  }
}
